package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.NameDouble;
import com.kaizzi.data.common.been.SeriesData;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Slf4j
@Component
public class Template35 {

    @Resource
    private TemplateUtil templateUtil;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    System.out.println(textRun.getRawText());
                    if (textRun.getRawText().equals("29KEY-1")){
                        if (data.get("pageStatistics").size() < 1){
                            textRun.setText("");
                        }
                        textRun.setText(data.get("pageStatistics").get(0).asText());
                        break;
                    }
                    if (textRun.getRawText().equals("29KEY-2")){
                        if (data.get("pageStatistics").size() < 2){
                            textRun.setText("");
                        }
                        textRun.setText(data.get("pageStatistics").get(1).asText());
                        break;
                    }
                    if (textRun.getRawText().equals("29KEY-3")){
                        textRun.setText(data.get("title").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("29KEY-4")){
                        if (data.get("pageStatistics").size() < 3){
                            textRun.setText("");
                        }
                        textRun.setText(data.get("pageStatistics").get(2).asText());
                        break;
                    }
                }catch (Exception e){
//                    logService.setDataLogStatus("ppt-error");
                    textRun.setText("");
                    log.error("page:{},text Exception:{}",35,e);
                }
            }
        }
    }

    public void simpleShape(int red, XSLFShape shape, JsonNode data) {
        if (red ==0 || red %10 !=0){
            return;
        }
        String jsonKey = "";
        String jsonKey1 = "";
        switch (red){
            case 10:
                jsonKey = "index";
                jsonKey1 = "pvs";
                break;
            case 20:
                jsonKey = "index";
                jsonKey1 = "uvs";
                break;
            case 30:
                jsonKey = "index";
                jsonKey1 = "avgs";
                break;
            case 40:
                jsonKey = "clockIn";
                jsonKey1 = "pvs";
                break;
            case 50:
                jsonKey = "clockIn";
                jsonKey1 = "uvs";
                break;
            case 60:
                jsonKey = "clockIn";
                jsonKey1 = "avgs";
                break;
            case 70:
                jsonKey = "teamSquare";
                jsonKey1 = "pvs";
                break;
            case 80:
                jsonKey = "teamSquare";
                jsonKey1 = "uvs";
                break;
            case 90:
                jsonKey = "teamSquare";
                jsonKey1 = "avgs";
                break;
            case 100:
                jsonKey = "pointMarket";
                jsonKey1 = "pvs";
                break;
            case 110:
                jsonKey = "pointMarket";
                jsonKey1 = "uvs";
                break;
            case 120:
                jsonKey = "pointMarket";
                jsonKey1 = "avgs";
                break;
            case 130:
                jsonKey = "cashMarket";
                jsonKey1 = "pvs";
                break;
            case 140:
                jsonKey = "cashMarket";
                jsonKey1 = "uvs";
                break;
            case 150:
                jsonKey = "cashMarket";
                jsonKey1 = "avgs";
                break;
            case 160:
                jsonKey = "club";
                jsonKey1 = "pvs";
                break;
            case 170:
                jsonKey = "club";
                jsonKey1 = "uvs";
                break;
            case 180:
                jsonKey = "club";
                jsonKey1 = "avgs";
                break;
            case 190:
                jsonKey = "pointStep";
                jsonKey1 = "pvs";
                break;
            case 200:
                jsonKey = "pointStep";
                jsonKey1 = "uvs";
                break;
            case 210:
                jsonKey = "pointStep";
                jsonKey1 = "avgs";
                break;
            case 220:
                jsonKey = "pointLog";
                jsonKey1 = "pvs";
                break;
            case 230:
                jsonKey = "pointLog";
                jsonKey1 = "uvs";
                break;
            case 240:
                jsonKey = "pointLog";
                jsonKey1 = "avgs";
                break;
            default:
                break;
        }
        int trend = getSimpleData(data,jsonKey,jsonKey1);
        templateUtil.replaceSimpleShape(trend,shape);
    }

    private static int getSimpleData(JsonNode data, String jsonKey, String jsonKey1){
        return data.get(jsonKey1).get(1).get(jsonKey).get("trend").asInt();
    }

    public void table(XSLFShape shape, JsonNode data) {
        TableShape tableShape = (TableShape)shape;
        int numberOfColumns = tableShape.getNumberOfColumns();
        int numberOfRows = tableShape.getNumberOfRows();
        List<String> dateList = DateUtil.getDateList(2);
        //遍历获取每一个单元格
        for (int col =0 ;col < numberOfColumns; col++){
            for (int row = 0; row<numberOfRows; row++ ){
                TableCell cell = tableShape.getCell(row, col);
                if (cell.getTextParagraphs().size() > 0){
                    XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
                    if (cellParagraphs.getTextRuns().size()> 0){
                        XSLFTextRun textRun = cellParagraphs.getTextRuns().get(0);
                        if (row == 0 ){
                            if (col == 0){
                                textRun.setText(dateList.get(0));
                            }
                            continue;
                        }
                        if (row == 4){
                            if (col == 0){
                                textRun.setText(dateList.get(1));
                            }
                            continue;
                        }
                        if (col == 0){
                            continue;
                        }
//                        System.out.println(StringUtil.decimalFormat(getCellData(data,row,col)));
                        textRun.setText(StringUtil.decimalFormat(getCellData(data,row,col)));
                    }
                }
            }
        }
    }

    private double getCellData(JsonNode data,int row,int col) {
        int sort = 0;
        String jsonKey;
        String jsonKey1;

        if (row > 4){
            sort = 1;
        }

        if (row == 1 || row == 5){
            jsonKey = "pvs";
        }else if (row == 2 || row == 6){
            jsonKey = "uvs";
        }else {
            jsonKey = "avgs";
        }

        switch (col){
            case 1:
                jsonKey1 = "index";
                break;
            case 2:
                jsonKey1 = "clockIn";
                break;
            case 3:
                jsonKey1 = "teamSquare";
                break;
            case 4:
                jsonKey1 = "pointMarket";
                break;
            case 5:
                jsonKey1 = "cashMarket";
                break;
            case 6:
                jsonKey1 = "club";
                break;
            case 7:
                jsonKey1 = "pointStep";
                break;
            case 8:
                jsonKey1 = "pointLog";
                break;
            default:
                jsonKey1 = "";
                break;
        }
        //方便复用 此处不进行格式化
        return data.get(jsonKey).get(sort).get(jsonKey1).get("value").asDouble();
    }

    public void barChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        List<SeriesData> barData = getBarData(data);
        templateUtil.barChart(plot,barData,workbook,sheet,chart);
    }

    private List<SeriesData> getBarData(JsonNode data) {
        List<Integer> rows = Arrays.asList(2, 6);
        List<Integer> cols = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
        List<String> nameList = Arrays.asList("首页", "打卡页", "一件组队广场页", "积分商城", "现金商城", "G乐部", "积步得分", "积分记录");
        List<String> dateList = DateUtil.getDateList(2);

        List<SeriesData> seriesDatas = new ArrayList<>();
        for (int i = 0; i < rows.size(); i++) {
            List<NameDouble> nameDoubles = new ArrayList<>();
            for (Integer col : cols) {
                NameDouble nameDouble = new NameDouble(nameList.get(col-1),getCellData(data,rows.get(i),col));
                nameDoubles.add(nameDouble);
            }
            SeriesData seriesData = new SeriesData(dateList.get(i),nameDoubles,"");
            seriesDatas.add(seriesData);
        }
        return seriesDatas;
    }
}
